package day_2022_4_to_7.day48;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 跳石板
 * @author haomin
 * @date 2022/05/09 09:14
 **/
public class Day13_2_JumpStoneSlab {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int count = fun(m,n);
        if(count == Integer.MAX_VALUE){
            System.out.println(-1);
        }else {
            System.out.println(count);
        }
    }

    private static int fun(int m, int n) {
        int dp[] = new int[n + 1];
        // dp[m] 是初始值，所以为0
        for (int i = m+1; i <= n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        for (int i = m; i < n-1; i++) {
            if(dp[i] == Integer.MAX_VALUE) continue;
            ArrayList<Integer> list = allFactor(i);
            for (int j = 0; j < list.size(); j++) {
                int temp = i + list.get(j);
                int count = dp[i] + 1;
                if(temp <= n && dp[temp] > count){
                    dp[temp] = count;
                }
            }
        }
        return dp[n];
    }

    public static ArrayList<Integer> allFactor(int n) {
        ArrayList list = new ArrayList();
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                list.add(i);
                if(n/i != i){
                    list.add(n/i);
                }
            }
        }
        return list;
    }
}